package com.itheima.ssm.controller;

import com.itheima.ssm.domain.Product;
import com.itheima.ssm.service.IProductService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import java.util.List;

@Controller
@RequestMapping("/product")
public class ProductController {
@Autowired
private IProductService productService;


//查询全部
@RequestMapping("/findAll")
    public ModelAndView findAll() throws Exception {
        ModelAndView mv =new ModelAndView();
        List<Product> all = productService.findAll();
        mv.addObject("productList",all);
        mv.setViewName("product_list1");
        return  mv;
    }

    //添加
    @RequestMapping("/save")
    public String save(Product product){
productService.save(product);
return "redirect:findAll";
    }

    //转修改页面

    @RequestMapping("/touodate")
    public ModelAndView toupdate(String id) throws Exception {
        Product product = productService.findById(id);
        ModelAndView mv = new ModelAndView();
        mv.addObject("product",product);
        mv.setViewName("product-update");
        return mv ;
    }


//修改
    @RequestMapping("/update")
    public String update( Product product){
    productService.update(product);
return "redirect:findAll";
    }


    //根据删除选中(根据id删除)

    @RequestMapping("/delete")
    public String delete(String[] ids ) throws Exception {
        for (String id : ids) {
            productService.deleteById(id);
        }

        return"redirect:findAll";
    }

}
